package com.xunyin.modules.area.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.area.domain.TAreaSpace;
import com.xunyin.modules.area.param.TAreaQueryParam;
import com.xunyin.modules.area.service.TAreaSpaceService;
import com.xunyin.modules.area.service.mapper.TAreaSpaceMapper;
import com.xunyin.modules.area.vo.TAreaSpaceQueryVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author admin
 * @date 2022-09-06
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TAreaSpaceServiceImpl extends BaseServiceImpl<TAreaSpaceMapper, TAreaSpace> implements TAreaSpaceService {

    private final IGenerator generator;

    private final TAreaSpaceMapper tAreaSpaceMapper;

    @Override
    public List<TAreaSpaceQueryVo> getList(TAreaQueryParam areaQueryParam) {
        LambdaQueryWrapper<TAreaSpace> wrapper = new LambdaQueryWrapper<>();
        if (areaQueryParam.getAreaLevel() != null) {
            wrapper.eq(TAreaSpace::getAreaLevel, areaQueryParam.getAreaLevel());
        }
        wrapper.orderByDesc(TAreaSpace::getAreaId);
        List<TAreaSpaceQueryVo> list = generator.convert(baseMapper.selectList(wrapper), TAreaSpaceQueryVo.class);
        return list;
    }

    /**
     * 首页-根据城市获取城市信息
     *
     * @param city
     * @return TAreaSpaceQueryVo
     */
    @Override
    public TAreaSpaceQueryVo getInfoCity(String city) {
        LambdaQueryWrapper<TAreaSpace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TAreaSpace::getAreaName, city)
                .ne(TAreaSpace::getAreaParent, 0)
                .last("limit 1");
        return generator.convert(this.getOne(wrapper), TAreaSpaceQueryVo.class);
    }

    /**
     * 获取省、市、区县列表
     *
     * @param areaParent
     * @return List<TAreaSpaceQueryVo>
     */
    @Override
    public List<TAreaSpaceQueryVo> listAreaByCode(Integer areaParent) {
        LambdaQueryWrapper<TAreaSpace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TAreaSpace::getAreaParent, areaParent).orderByDesc(TAreaSpace::getAreaId);
        List<TAreaSpaceQueryVo> list = generator.convert(baseMapper.selectList(wrapper), TAreaSpaceQueryVo.class);
        return list;
    }

    @Override
    public TAreaSpace getTAreaSpaceByAreaCode(String areaCode) {
        LambdaQueryWrapper<TAreaSpace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TAreaSpace::getAreaCode, areaCode)
                .last("limit 1");
        return tAreaSpaceMapper.selectOne(wrapper);
    }


}
